package com.serial.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.serial.activity.R;
import com.serial.entity.User;

import java.util.List;

public class AuthAdapter extends BaseAdapter {

    private Context context;
    private List<User> dataList;
    private int resources;
    private LayoutInflater inflater;
    private OnCheckChanged onCheckChanged;

    public void setOnCheckChanged(OnCheckChanged onCheckChanged) {
        this.onCheckChanged = onCheckChanged;
    }

    public AuthAdapter(Context context, int resources) {
        this.context = context;
        this.resources = resources;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public interface OnCheckChanged{
        void onCheckChange(int postion,boolean isSelect);
    }

    public List<User> getDataList() {
        return dataList;
    }

    public void setDataList(List<User> dataList) {
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView textView;
        CheckBox checkBox;
        ImageView imageView;
        if (convertView == null){
            convertView = inflater.inflate(resources,null);
            textView = convertView.findViewById(R.id.text_name);
            checkBox = convertView.findViewById(R.id.img_select);
            imageView = convertView.findViewById(R.id.img_debug);
            convertView.setTag(new ViewHolder(textView,checkBox,imageView));
        }else {
            ViewHolder view = (ViewHolder) convertView.getTag();
            textView = view.textView;
            checkBox = view.checkBox;
            imageView = view.imageView;
        }
        User u = dataList.get(position);
        textView.setText(u.getName());
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                onCheckChanged.onCheckChange(position,isChecked);
            }
        });
        if (u.isAuthorize()){
            checkBox.setChecked(true);
        }else {
            checkBox.setChecked(false);
        }
        if (u.isDebug()){
            Glide.with(context).load(R.drawable.debug_on).into(imageView);
        }else {
            Glide.with(context).load(R.drawable.debug_off).into(imageView);
        }

        return convertView;
    }

    class ViewHolder{
        public TextView textView;
        public CheckBox checkBox;
        public ImageView imageView;
        public View view;
        public ViewHolder(TextView textView,CheckBox checkBox,ImageView imageView) {
            this.textView = textView;
            this.checkBox = checkBox;
            this.imageView = imageView;
        }
    }
}
